<?php
/**
 * food category and food option category relation
 * food category (unique in database) <(one-many) --- (one-one)> food option category (mutiple in database)
 */
class category_relation_model extends CI_Model {
	private $error = array();
    private $is_valid_schema = false;
    /**
	 * constructor to load database object
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->database();
        $this->load->model('food_category_model', 'food_category');
        $this->load->model('food_option_category_model', 'food_option_category');
	}

    /**
     * @param $food_category_id
     * @param $food_option_category_id
     * @return array
     */
    function create($food_category_id, $food_option_category_id)
    {
        $this->error = array('user' => array(), 'system' => array());
        // prepare data
        $food_category_id = trim($food_category_id);
        $food_option_category_id = trim($food_option_category_id);

        // unique food category id check
        $relation = $this->read($food_category_id);
        if(array_key_exists('food_category_id', $relation) === true){
            $this->error['user']['create_action'] = 'relation already exist with target food category';
            return $this->error;
        }

        $this->_validate_schema($food_category_id, $food_option_category_id);
		if(true === $this->is_valid_schema){
            $data =  array(
                'food_category_id' => $food_category_id,
                'food_option_category_id' => $food_option_category_id
            );
            $this->db->insert('food_option_category_relation', $data);
        }
        return $this->error;
    }
    /**
     * @param $food_category_id
     * @return array $result
     */
    function read($food_category_id)
    {
        $this->db->limit(1);
		$this->db->where('food_category_id', $food_category_id);
		$query = $this->db->get('food_option_category_relation');
		$result = $query->row_array();

        return $result;
    }
    /**
     * @param $food_category_id
     * @param $food_option_category_id
     * @return array $error
     */
    function update($food_category_id, $food_option_category_id)
    {
        $this->error = array('user' => array(), 'system' => array());

        // prepare data
        $food_category_id = trim($food_category_id);
        $food_option_category_id = trim($food_option_category_id);

        $this->_validate_schema($food_category_id, $food_option_category_id);
		if(true === $this->is_valid_schema){
            $data =  array(
                'food_option_category_id' => $food_option_category_id
            );
            $this->db->where('food_category_id', $food_category_id);
            $this->db->update('food_option_category_relation', $data);
        }
        return $this->error;
    }
    /**
     * @param $food_category_id
     * @return array
     */
    function delete($food_category_id)
    {
        $this->error = array('user' => array(), 'system' => array());

        $this->db->where('food_category_id', $food_category_id);
		$this->db->delete('food_option_category_relation');

        return $this->error;
    }
    /**
     * @param $food_category_id
     * @param $food_option_category_id
     * @return void
     */
    private function _validate_schema($food_category_id, $food_option_category_id)
    {
        $this->is_valid_schema = false;

        $user_error_intro_count = count($this->error['user']);

        // id validation check
        $this->load->model('food_category_model', 'food_category');
        $this->load->model('food_option_category_model', 'food_option_category');

        $food_category = $this->food_category->read($food_category_id);
        $food_option_category = $this->food_option_category->read($food_option_category_id);
                
        if(count($food_category) === 0){
            $this->error['user']['food_category'] = 'food category id not exist';
        }

        if(count($food_option_category) === 0){
            $this->error['user']['food_option_category'] = 'food option category id not exist';
        }

        $user_error_outro_count = count($this->error['user']);

        if($user_error_intro_count === $user_error_outro_count){
            $this->is_valid_schema = true;
        }
    }
}

/* End of file category_relation_model.php */
/* Location: ./system/application/models/category_relation_model.php */
